package com.hystrix.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * 普通fallback返回默认值
 * 
 * 除了HystrixBadRequestException异常之外，所有从run()方法抛出的异常都算作失败，并触发降级getFallback()和断路器逻辑。
 * 
 * @author wangxh
 *
 */
public class FallBackCommand extends HystrixCommand<String> {

	private String name;

	public FallBackCommand(String name) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("FallBackGroupCommandKey"))
				.andCommandPropertiesDefaults(
						HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(500)));
		this.name = name;
	}

	@Override
	protected String getFallback() {
		return "Executed  Fallback!";
	}

	@Override
	protected String run() throws Exception {
		Thread.sleep(1000l);
		return "Hello " + name + " thread:" + Thread.currentThread().getName();
	}

}
